<?php
/**
 * Date: 16/8/15
 * Time: 下午8:18
 */

namespace Dao6\Cache;

/**
 * Memcached 缓存
 * @package Dao6\Cache
 */
class MemcacheCache implements ICache{

    private $memcache;

    public function __construct($host = '127.0.0.01', $port = 11211) {
        if(class_exists('Memcached')){
            $this->memcache = new \Memcached();
            $this->memcache->addServer($host, $port);
        }else if(class_exists('Memcache')){
            $this->memcache = new \Memcache();
            $this->memcache->connect($host, $port);
        }else{
            throw new \ErrorException('class \Memcache not found.');
        }
    }

    public function set($name, $value, $exp = 0) {
        return $this->memcache->set($name, $value, $exp);
    }

    public function get($name, $defaultValue = null) {
        $value = $this->memcache->get(md5($name));
        return $value ? $value : $defaultValue;
    }
}